<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */



/*
 * create or update country information 
 * 
 * @param args = {
 * 
 *  @param id
 *  @param name
 * 
 * } 
 */
function countries_save($args){

    //import required global variables 
    global $db;
    global $contex;
    
    //extract and sanitize parameters creating a variable for each
    $fields = X_param_array ($args, 'fields'); 
    $id = X_param ($args, 'id'); 
    $data['name'] = X_param ($args, 'name'); if(!$id && !$data['name']) return __MOOV_PARAM_INVAL_;
    $data['added_ts'] = X_param ($args, 'added_ts', gmdate('y-m-d h:m:s'));
    $data['modified_ts'] = X_param ($args, 'modified_ts', gmdate('y-m-d h:m:s'));
    $data['status'] = X_param ($args, 'status', 'A'); 
    $limit = X_param_array ($args, 'limit');
    $sort = X_param_array ($args, 'sort');
    
    // ---------------- Operation begins here ---------------------
    $id = $db->save('countries',$id,$data);
    return countries_list(array('id' => $id, 'fields' => $fields, 'limit' => $limit, 'sort' => $sort));  
       
}

/*
 * List one or more country on the platform
 * 
 * @param args = {
 * 
 *  @param fields
 *  @param id
 *  @param name
 *  @param added_ts
 *  @param modified_ts
 *  @param status
 * 
 * }  
 */
function countries_list($args){
   
    //import required global variables 
    global $db;
    global $contex;

    //extract and sanitize parameters creating a variable for each
    $fields = X_param_array ($args, 'fields'); 
    $data['id'] = X_param_array ($args, 'id'); 
    $data['name'] = X_param_array ($args, 'name'); 
    $data['added_ts'] = X_param ($args, 'added_ts');
    $data['modified_ts'] = X_param ($args, 'modified_ts');
    $data['status'] = X_param_array ($args, 'status'); 
    $limit = X_param_array ($args, 'limit');
    $sort = X_param_array ($args, 'sort');
    
    
    // ---------------- Operation begins here ---------------------
    $rc = $db->get('countries', $fields, $data, $limit, $sort);
    return out($rc);    
}